import os
from langfuse import get_client
from langfuse.langchain import CallbackHandler
from langfuse import Langfuse
from loguru import logger

def get_langfuse_client() -> Langfuse:
    if not all(os.getenv(k) for k in ["LANGFUSE_HOST", "LANGFUSE_PUBLIC_KEY", "LANGFUSE_SECRET_KEY"]):
        return None
    return get_client()

def get_langfuse_callback():
    return CallbackHandler() if get_langfuse_client() else None

def get_langfuse_prompt(name: str) -> str:
    try:
        if client := get_langfuse_client():
            sanitized_name = name.replace('/', '#').replace('\\', '#')
            if prompt := client.get_prompt(sanitized_name):
                return prompt.prompt
    except Exception as e:
        pass
    return None
